<%@page import="deptentity.DeptComponent"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="empsession.EmpSessionHome"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="empsession.EmpSessionComponent"%>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%!    private EmpSessionComponent empSession = null;
    private String ejbsessionname = "ejb/EmpSessionBean";

    public void jspInit() {
        try {
            InitialContext context = new InitialContext();
            EmpSessionHome empSessionHome = (EmpSessionHome) context.lookup(ejbsessionname);
            empSession = empSessionHome.create();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
%>
<%
    String deptno = (String) request.getParameter("id");
    DeptComponent dept = empSession.getDeptByDeptno(new BigDecimal(deptno));
    extras.Log log = (extras.Log) session.getAttribute("log");
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="styles.css"/>
        <title>Отдел</title>
    </head>
    <body>
        <div class="navigation">
            <%@include file="navigation.jspf"%>
        </div><br/>
        <h1>Просмотр информации об отделе</h1>
        <% if (log != null) {%>
        <div class="<%= log.isCorrect() ? "log-correct" : "log-incorrect"%>"><%= log.getMessage()%></div>
        <% } else {%><br/><% }%>
        <br/>

        <table>
            <tr>
                <th colspan="2">Отдел</th>
            </tr>
            <tr>
                <th>Название</th>
                <td><%= dept.getDname()%></td>
            </tr>
            <tr>
                <th>Расположение</th>
                <td><%= dept.getLoc()%></td>
            </tr>
        </table>
        <input type="submit" name="edit" value="Редактировать" onclick="self.location='editDept.jsp?id=<%= dept.getDeptno()%>'">
        <input type="submit" name="delete" value="Удалить" onclick="self.location='viewDept.jsp?delete=on&id=<%= dept.getDeptno()%>'">
        <%
            session.removeAttribute("log");
            try {
                if (request.getParameter("delete") != null) {
                    empSession.deleteDept(dept.getDeptno());
                    session.setAttribute("log", new extras.Log(true, "", "Запись удалена"));
                    response.sendRedirect("findDept.jsp");
                }
            } catch (Throwable ex) {
                session.setAttribute("log", new extras.Log(false, "", "Удаление записи невозможно!"));
                response.sendRedirect("findDept.jsp");
            }
        %>
    </body>
</html>
